import { USER_LOGIN, INIT_USER } from "./mutations-types";
import { MessageBox } from "mint-ui";
export default {
  state: {
    //登陆状态
    loginStatus: false,
    //token
    token: null,
    //用户信息：用户头像|用户信息
    userInfo: {},
  },
  getters: {},
  mutations: {
    //设置
    [USER_LOGIN](state, user) {
      state.loginStatus = true;
      state.token = user.token;
      state.userInfo = user;

      //持久化存储==》本地存储
      localStorage.setItem("UserInfo", JSON.stringify(user));
    },
    //读取
    [INIT_USER](state) {
      let userInfo = JSON.parse(localStorage.getItem("UserInfo"));
      if (userInfo) {
        state.loginStatus = true;
        state.token = userInfo.token;
        state.userInfo = userInfo;
      }
    },
    //退出登录
    loginOut(state) {
      MessageBox({
        title: "提示",
        message: "你确定要退出吗?",
        showCancelButton: true,
      }).then((res) => {
        if (res == "confirm") {
          state.loginStatus = false;
          state.token = null;
          state.userInfo = {};
          localStorage.removeItem("UserInfo");
        }
      });
    },
  },
  actions: {},
};
